﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class FlowModelList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] == null || Session["roleID"] == null)
        {
            Response.Redirect("Login.aspx");
            Response.End();
        }        
        if (!Page.IsPostBack)
        {
            BindToDdlFlowMType();
            if (Request.QueryString["flowtypeid"] != null)
            {
                ddlFlowMType.SelectedValue = Request.QueryString["flowtypeid"].ToString();
            }
            if (Request.QueryString["flowmname"] != null)
            {
                txtFlowMName.Text = Request.QueryString["flowmname"].ToString();
            }
            BindToRpFlowMList();
        }

    }
    private void BindToDdlFlowMType()
    {
        string sqlString = "select * from FLOWTYPE order by FLOWTYPEID ASC";
        DBOperation DBO = new DBOperation();
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        ddlFlowMType.DataSource = sdr;
        ddlFlowMType.DataTextField = "TYPENAME";
        ddlFlowMType.DataValueField = "FLOWTYPEID";
        ddlFlowMType.DataBind();
        ddlFlowMType.Items.Insert(0, new ListItem("全部", "-1"));
        sdr.Close();
    }
    protected void BindToRpFlowMList()
    {
        RpFlowMList.DataSource = pds();
        RpFlowMList.DataBind();
    }
    private PagedDataSource pds()
    {
        int pageSize = 15;
        string flowTypeID = ddlFlowMType.SelectedValue.ToString();
        string flowMName = txtFlowMName.Text.ToString();        
        string condition = " where 1=1 ";
        if (flowTypeID != "-1")
        {
            condition += "and FLOWTYPEID=" + flowTypeID + " ";

        }
        if (flowMName != "")
        {
            condition += "and FLOWMNAME like '%" + flowMName + "%'";
        }        
        string sqlString = "select * from LCMINFO" + condition + " order by FLOWTYPEID asc,ADDTIME DESC";
        DBOperation DBO = new DBOperation();
        DataSet ds = DBO.GetDataSet(sqlString, "temp");
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables["temp"].DefaultView;
        pds.AllowPaging = true;//允许分页
        pds.PageSize = pageSize;//单页显示项数
        int CurrentPage = 0;
        if (Request.QueryString["page"] != null)
        {
            CurrentPage = Convert.ToInt32(Request.QueryString["page"]);
        }
        pds.CurrentPageIndex = CurrentPage;
        return pds;
    }
    protected void ddlp_SelectedIndexChanged(object sender, EventArgs e)
    {//脚模板中的下拉列表框更改时激发
        string searchCondition = "&flowtypeid=" +ddlFlowMType.SelectedValue + "&flowmname=" +txtFlowMName.Text.ToString().Trim() ;
        string pg = Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue) - 1));//获取列表框当前选中项
        Response.Redirect("FlowModelList.aspx?page=" + pg + searchCondition);//页面转向
    }

    protected void RpYCList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        string searchCondition = "&flowtypeid=" + ddlFlowMType.SelectedValue + "&flowMname=" + txtFlowMName.Text.ToString().Trim();
        if (e.Item.ItemType == ListItemType.Footer)
        {
            DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");

            HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");
            HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");
            HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");
            HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");

            pds().CurrentPageIndex = ddlp.SelectedIndex;

            int n = Convert.ToInt32(pds().PageCount);//n为分页数
            int i = Convert.ToInt32(pds().CurrentPageIndex);//i为当前页

            Label lblpc = (Label)e.Item.FindControl("lblpc");
            lblpc.Text = n.ToString();
            Label lblp = (Label)e.Item.FindControl("lblp");
            lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);

            if (!IsPostBack)
            {
                for (int j = 0; j < n; j++)
                {
                    ddlp.Items.Add(Convert.ToString(j + 1));
                }
            }

            if (i <= 0)
            {
                lpfirst.Enabled = false;
                lpprev.Enabled = false;
                lplast.Enabled = true;
                lpnext.Enabled = true;
            }
            else
            {
                lpprev.NavigateUrl = "?page=" + (i - 1) + searchCondition;
            }
            if (i >= n - 1)
            {
                lpfirst.Enabled = true;
                lplast.Enabled = false;
                lpnext.Enabled = false;
                lpprev.Enabled = true;
            }
            else
            {
                lpnext.NavigateUrl = "?page=" + (i + 1) + searchCondition;
            }

            lpfirst.NavigateUrl = "?page=0" + searchCondition;//向本页传递参数page
            lplast.NavigateUrl = "?page=" + (n - 1) + searchCondition;

            ddlp.SelectedIndex = Convert.ToInt32(pds().CurrentPageIndex);//更新下拉列表框中的当前选中页序号
        }
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button btnDel = (Button)e.Item.FindControl("btnDel");
           btnDel.Attributes.Add("onclick", "return confirm('你确定要删除该流程模板吗？');");
           if (Session["roleID"].ToString() != "1")
           {
               btnDel.Text = "无权限";
               btnDel.Enabled = false;
           }
        }
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string flowTypeId = ddlFlowMType.SelectedValue;
        string flowMname = txtFlowMName.Text;
        string toURL = "FlowModelList.aspx?flowtypeid=" + flowTypeId + "&flowmname=" + flowMname;
        Response.Redirect(toURL);
    }
    protected void RpFlowMList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int flowMID = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "del")
        {
            FlowModel tempFlowModel = new FlowModel(flowMID);
            tempFlowModel.DelFlowModel();
            BindToRpFlowMList();

        }
    }
}
